#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem :  con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    vector<int> number = {1, 3, 5, 7, 9, 8, 6, 4, 2, 10};
    display(number);
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的尾部进行插入" << endl;
    number.push_back(200);
    number.push_back(300);
    display(number);
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl << "在vector的任意位置进行插入" << endl;
    //因为push_back每次插入元素的个数是固定的，那么只要老的空间
    //大小大于等于1，那么两倍扩容肯定就没有问题
    //因为vector的insert插入的元素的个数是不确定，所以不能按照
    //一个统一的方式进行扩容
    //size() = m = 12, capacity() = n = 20,新插入的元素个数t
    //1、t < n - m;此时不会进行扩容
    //2、n - m < t < m,会按照 2 * size()进行扩容
    //3、n - m < t, m < t < n，会按照t + m进行扩容
    //4、n - m < t, t > n，会按照t + m进行扩容
    auto it = number.begin();
    it += 2;
    cout << "*it = " << *it << endl;
    number.insert(it, 30, 666);
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;

    cout << endl;
    it = number.begin();
    it += 2;
    cout << "*it = " << *it << endl;
    vector<int> vec = {10, 30, 50, 20};
    number.insert(it, vec.begin(), vec.end());
    display(number);
    cout << "*it = " << *it << endl;
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

